//申请的BING地图密匙
//Cesium.BingMapsApi.defaultKey = 'AqYgyS1gIIDGsxmOlncqrgA83cHnhClLwZmIJXbYXX36pfxKYtUGk12Q3splaf4Y';
//Cesium.BingMapsApi.defaultKey = Earth_CONFIG.BingMapsKey;

//用于构建应用程序的基础小部件 
var viewer;
var imageLayer;
var terrainLayer;
var map;
var modelDataSourceLayer;

var cgs2000Ellipsolid = new Cesium.Ellipsoid(6378137.0, 6378137.0, 6356752.31414035585);
var myGeographicTilingScheme = new Cesium.GeographicTilingScheme({
    ellipsoid: cgs2000Ellipsolid,
    rectangle: Cesium.Rectangle.fromDegrees(-180, -90, 180, 90),
    numberOfLevelZeroTilesX: 4,
    numberOfLevelZeroTilesY: 2
});
var baseplatformurl,databases,layerinfo;

function earthInit() {
    modelDataSourceLayer = new Cesium.CustomDataSource("modelDataSourceLayer");
    Cesium.Ion.defaultAccessToken = m_MapConfigLayers.cesiumAsset;
    //设置Cesium球投影
    var obj = [6378137.0, 6378137.0, 6356752.3142451793]; //椭球体
    //	viewer设置
    var options = {
        imageryProvider: new Cesium.TileMapServiceImageryProvider({
            url: Cesium.buildModuleUrl('Assets/Textures/NaturalEarthII')
        }),
        sceneModePicker: false,
        baseLayerPicker: false,
        mapMode2D: Cesium.MapMode2D.ROTATE, //2D地图可以在水平方向无限滚动
        vrButton: false, //VR
        navigation: true,
        terrainExaggeration: 1, //地形高程比例
        shadows: false, //确定阴影是否由太阳投下
        terrainShadows: Cesium.ShadowMode.DISABLED, //确定地形是否投射或接收来自太阳的阴影
        timeline: false, //时间线
        navigationHelpButton: false, //使用帮助
        //imageryProviderViewModels: [img_arcgis_yxdt, img_arcgis_jcdt, img_tdt_sl,img_tdt_yx],//可供BaseLayerPicker选择的图像图层ProviderViewModel数组
        //selectedImageryProviderViewModel: img_arcgis_yxdt,
        geocoder: false, //地理编码
        homeButton: false,
        selectionIndicator: false,
        infoBox: false,
        fullscreenButton: false,
        contextOptions: {
            webgl: {
                alpha: true,
                depth: true,
                stencil: true,
                antialias: true,
                premultipliedAlpha: true,
                //通过canvas.toDataURL()实现截图需要将该项设置为true
                preserveDrawingBuffer: true,
                failIfMajorPerformanceCaveat: true
            }
        }
        //mapProjection: cgs2000GeographicProj//设置viewer坐标系
    };
    viewer = new Cesium.Viewer('cesiumContainer', options);
    viewer.scene.postProcessStages.fxaa.enabled = true; //是否开启抗锯齿
    viewer.scene.fxaa = true; //是否开启抗锯齿
    viewer.scene.fog.enabled = false; //去除云雾效果
    viewer.scene.skyAtmosphere.show = !1;
    // 亮度设置
    var stages = viewer.scene.postProcessStages;
    viewer.scene.brightness = viewer.scene.brightness || stages.add(Cesium.PostProcessStageLibrary.createBrightnessStage());
    viewer.scene.brightness.enabled = true;
    viewer.scene.brightness.uniforms.brightness = Number(1.5);
    //以下代码解决画面显示模糊，锯齿明显问题。
    viewer._cesiumWidget._supportsImageRenderingPixelated = Cesium.FeatureDetection.supportsImageRenderingPixelated();
    viewer._cesiumWidget._forceResize = true;
    if (Cesium.FeatureDetection.supportsImageRenderingPixelated()) {
        var vtxf_dpr = window.devicePixelRatio;
        // 适度降低分辨率
        while (vtxf_dpr >= 2.0) {
            vtxf_dpr /= 2.0;
        }
        viewer.resolutionScale = vtxf_dpr;
    }
    viewer.id = "cesiumContainer";
    //var cesiumWidget = new Cesium.CesiumWidget('cesiumContainer');
    var options = {};
    options.enableCompass = true; //罗盘
    options.enableZoomControls = true; //缩放
    options.enableDistanceLegend = true; //比例尺
    options.enableCompassOuterRing = true; //指南针外环
    viewer.extend(Cesium.viewerCesiumNavigationMixin, options);
    show3DLayersMap(m_MapConfigLayers.maplayers);
    //初始化 CesiumToolbar
    //CesiumToolbar.init();
    viewer.scene.postProcessStages.removeAll();
    //viewer.geocoder.viewModel.geoKey = 'fvV2osxwuZWlY0wJb8FEb2i5';
    viewer.scene.globe.depthTestAgainstTerrain = true;
    viewer.scene.postProcessStages.fxaa.enabled = false; //去锯齿 使文字清晰
    viewer.scene.fxaa = false;
    viewer.dataSources.add(modelDataSourceLayer);

    var homeview = window.setTimeout(function() {
        imageLayer = viewer.imageryLayers._layers[0];
        terrainLayer = viewer.terrainProvider;
        MapStatic.flyTo({ lon: m_MapConfigLayers.mapconfig.center[0], lat: m_MapConfigLayers.mapconfig.center[1], height: m_MapConfigLayers.mapconfig.viewheight });
        clearTimeout(homeview);
        addEvent();
    }, 500);
    $('.loadingIndicator').css('display', 'none');
    // forbidDown();
    // onViewerLoad();
}

function getAllDatabase(){
    if(!baseplatformurl){
        baseplatformurl = getBaseplatformPort();
    }
    var data;
    $.ajax({
        type: "post",
        async: false,
        crossDomain : true,
        xhrFields : {
            withCredentials: true
        },
        url: baseplatformurl + "dababaseConnect/getList",
        data: {
        },
        datatype: "json",
        success: function (result) {
            data = JSON.parse(result).data;
        },
        error: function (err) {
        }
    })
    return data;
}

function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) {
        //return unescape(r[2]);
        return r[2];
    }
    return null;
}